﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using System.Web.Mvc.Html;

using System.Security.Principal;

namespace Cipher.Web.MVC {
    public static class LinkExtension {
        //private static TableDefinesDataContext _context = new TableDefinesDataContext();

        public static bool HasPemission(this HtmlHelper htmlHelper, string rule) {
            IPrincipal user = htmlHelper.ViewContext.HttpContext.User;

            if(user.IsInRole("Admin"))
                return true;

            //var roles = (from m in _context.RoleAuthorizationMaps
            //             where m.Rule == rule && (m.Allow == 1 || m.Allow == 2)
            //             select m.RoleName).ToList();

            //foreach(string r in roles) {
            //    if(user.IsInRole(r))
            //        return true;
            //}

            return false;
        }

        public static MvcHtmlString ActionAuthLink(this HtmlHelper htmlHelper, string rule, string linkText, string actionName, string controllerName) {
            if (!htmlHelper.ViewContext.HttpContext.User.Identity.IsAuthenticated)
                return MvcHtmlString.Empty;

            if(!htmlHelper.HasPemission(rule))
                return MvcHtmlString.Empty;

            return htmlHelper.ActionLink(linkText, actionName, controllerName);
        }

        public static MvcHtmlString ActionAuthLink(this HtmlHelper htmlHelper, string rule, 
            string linkText, 
            string actionName, 
            string controllerName,
            object routeValues,
            object htmlAttributes) {
            if (!htmlHelper.ViewContext.HttpContext.User.Identity.IsAuthenticated)
                return MvcHtmlString.Empty;

            if(!htmlHelper.HasPemission(rule))
                return MvcHtmlString.Empty;

            return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
        }

        public static MvcHtmlString ActionAuthLink(this HtmlHelper htmlHelper, string rule,
            string linkText,
            string actionName,
            string controllerName,
            object routeValues) {
            if (!htmlHelper.ViewContext.HttpContext.User.Identity.IsAuthenticated)
                return MvcHtmlString.Empty;

            if(!htmlHelper.HasPemission(rule))
                return MvcHtmlString.Empty;

            return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, new { });
        }

        public static MvcHtmlString ActionAuthLink(this HtmlHelper htmlHelper, string rule, 
            string linkText,
            string actionName,
            string controllerName,
            RouteValueDictionary routeValues,
            IDictionary<string, object> htmlAttributes) {
            if (!htmlHelper.ViewContext.HttpContext.User.Identity.IsAuthenticated)
                return MvcHtmlString.Empty;

            if(!htmlHelper.HasPemission(rule))
                return MvcHtmlString.Empty;

            return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
        }

        public static MvcHtmlString ActionAuthLink(this HtmlHelper htmlHelper, string rule, 
            string linkText,
            string actionName,
            string controllerName,
            RouteValueDictionary routeValues) {
            if (!htmlHelper.ViewContext.HttpContext.User.Identity.IsAuthenticated)
                return MvcHtmlString.Empty;

            if(!htmlHelper.HasPemission(rule))
                return MvcHtmlString.Empty;

            return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, new { });
        }
    }
}